package org.jeecg.modules.factor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.api.CyEmissionFactorApi;
import org.jeecg.modules.api.DTO.AddEmissionByCarbonCheckDTO;
import org.jeecg.modules.api.DTO.EmissionFactorDTO;
import org.jeecg.modules.api.DTO.EmissionFactorQueryDTO;
import org.jeecg.modules.api.DTO.PrivateFactorQueryDTO;
import org.jeecg.modules.api.VO.EmissionFactorDescribeVO;
import org.jeecg.modules.api.VO.EmissionFactorQueryVO;
import org.jeecg.modules.api.VO.EmissionFactorVO;
import org.jeecg.modules.api.entity.CyEmissionFactor;
import org.jeecg.modules.api.entity.CyEmissionUnit;
import org.jeecg.modules.api.entity.CyHouseGas;
import org.jeecg.modules.factor.service.ICyEmissionFactorCollectService;
import org.jeecg.modules.factor.service.ICyEmissionFactorService;
import org.jeecg.modules.factor.service.ICyEmissionUnitConvertService;
import org.jeecg.modules.factor.service.ICyHouseGasService;
import org.jeecg.modules.api.entity.CyGasStore;
import org.jeecg.modules.unitGroup.entity.CyFactorUnitGroup;
import org.jeecg.modules.unitGroup.service.ICyFactorUnitGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author mjh
 * Date: 2024/11/19 18:48
 */
@Api(tags="碳排因子api所使用的controller")
@RestController
@RequestMapping("/emission/factor/cyEmissionFactor/api")
@Slf4j
public class CyEmissionFactorApiController implements CyEmissionFactorApi {
    @Autowired
    private ICyEmissionFactorService cyEmissionFactorService;    //碳排因子Service层


    @Autowired
    private ICyEmissionFactorCollectService cyEmissionFactorCollectService;   //碳排因子收藏service层


    @Autowired
    private ICyFactorUnitGroupService cyFactorUnitGroupService;     //单位分组的service层


    @Autowired
    private ICyHouseGasService cyHouseGasService;    //碳排因子温室气体配比Service层

    @Autowired
    private ICyEmissionUnitConvertService cyEmissionUnitConvertService;    //碳排因子单位换算service层

    /**
     *  获取碳排因子详情
     *
     * @param id 碳排因子ID
     * @return emissionFactorVO 碳排因子详情对象
     */
    @Override
    @GetMapping(value = "/listInfo")
    public Result<EmissionFactorVO> listInfo(@RequestParam(name="id",required=true)String id) {
        EmissionFactorVO emissionFactorVO = cyEmissionFactorService.listInfo(id);
        return Result.OK(emissionFactorVO);
    }

    /**
     * 产品碳足迹和组织碳盘查中使用的分页列表查询
     *
     * @param privateFactorQueryDTO 分页列表查询条件dto
     * @param pageNo
     * @param pageSize
     * @return emissionFactorQueryVOIPage 碳排因子分页列表查询结果对象
     */
    @Override
    @GetMapping(value = "/privateList")
    public Result<?> privateQueryPageList(PrivateFactorQueryDTO privateFactorQueryDTO,
                                          @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

        IPage<EmissionFactorQueryVO> emissionFactorQueryVOIPage = cyEmissionFactorService.privateQueryPageList(privateFactorQueryDTO, pageNo, pageSize);
        return Result.OK(emissionFactorQueryVOIPage);
    }

    /**
     * 批量查询碳排因子使用说明对象 （产品碳足迹中使用）
     * @param idList 碳排因子id集合
     * @return emissionFactorDescribeVOS 碳排因子说明对象集合
     */
    @Override
    @GetMapping(value = "/describeList")
    public Result<List<EmissionFactorDescribeVO>> describeList(@RequestParam(name="idList",required=true) List<String> idList) {
        List<EmissionFactorDescribeVO> emissionFactorDescribeVOS = cyEmissionFactorService.describeList(idList);
        return Result.ok(emissionFactorDescribeVOS);
    }

    /**
     *  批量获取碳排因子详情
     *
     * @param idList 碳排因子id集合
     * @return  碳排因子详情集合
     */
    @Override
    @GetMapping(value = "/batchListInfo")
    public Result<List<EmissionFactorVO>> batchListInfo(@RequestParam(name="idList",required=true) List<String> idList) {

        //批量获取碳排因子详情
        List<EmissionFactorVO> emissionFactorVOS = cyEmissionFactorService.batchListInfo(idList);
        return Result.OK(emissionFactorVOS);
    }

    /**
     * 批量查询碳排因子主体信息
     *
     * @param idList 碳排因子id集合
     * @return 碳排因子主体信息集合
     */
    @Override
    @GetMapping(value = "/batchGetFactorInfo")
    public Result<List<CyEmissionFactor>> batchGetFactorInfo(@RequestParam(name="idList",required=true) List<String> idList) {
        //  批量查询碳排因子主体信息
        List<CyEmissionFactor> cyEmissionFactors = cyEmissionFactorService.batchGetFactorInfo(idList);
        return Result.OK(cyEmissionFactors);
    }


    /**
     *   碳排因子收藏
     *
     * @param emissionFactorId 碳排因子ID
     * @return
     */
    @Override
    @GetMapping(value = "/emissionCollect")
    public Result<String> emissionCollect(@RequestParam(name="id",required=true) String emissionFactorId) {
        String backMsg = cyEmissionFactorCollectService.add(emissionFactorId);
        return Result.OK(backMsg);
    }


    /**
     *   组织碳盘查中用到的手动添加碳排因子
     * @param addEmissionByCarbonCheckDTO 手动添加碳排因子的新增对象
     * @return
     */
    @Override
    @PostMapping(value = "/addEmissionByCarbonCheck")
    public Result<String> addEmissionByCarbonCheck(@RequestBody AddEmissionByCarbonCheckDTO addEmissionByCarbonCheckDTO) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();  //获取当前用户
        addEmissionByCarbonCheckDTO.setOrganizationId(sysUser.getOrganizationId());   //当前用户的组织ID
        addEmissionByCarbonCheckDTO.setDeptId(sysUser.getDeptId());   //当前用户的部门ID

        EmissionFactorDTO emissionFactorDTO = new EmissionFactorDTO();
        org.springframework.beans.BeanUtils.copyProperties(emissionFactorDTO, addEmissionByCarbonCheckDTO);
        cyEmissionFactorService.add(emissionFactorDTO);
        return Result.OK("添加成功！");
    }

    /**
     * 查询所有单位分组
     * @return allUnitGroup 所有单位分组集合
     */
    @Override
    @GetMapping(value = "/getAllUnitGroup")
    public Result<?> getAllUnitGroup(){
        // 查询所有单位分组
        List<CyFactorUnitGroup> allUnitGroup = cyFactorUnitGroupService.getAllUnitGroup();
        return Result.ok(allUnitGroup);
    }


    /**
     * 返回树形产品单位
     */
    @Override
    @GetMapping(value = "/getUnitTree")
    public Result<Map<String, List<CyEmissionUnit>>> getUnitTree(){
        Map<String, List<CyEmissionUnit>> unitTree = cyFactorUnitGroupService.getUnitTree();
        return Result.ok(unitTree);
    }


    /**
     * 根据碳排因子idList获取到所有碳排因子中的所有气体配比信息
     * @param EmissionFactorIdList 碳排因子id集合
     * @return gasRatioList 碳排因子气体配比集合
     */
    @Override
    @GetMapping(value = "/batchGetGasRatio")
    public Result<?> batchGetGasRatio(@RequestParam(name="idList",required=true) List<String> EmissionFactorIdList){
        //根据碳排因子id集合查询出其下所包含的所有气体配比
        List<CyHouseGas> gasRatioList = cyHouseGasService.batchGetGasRatio(EmissionFactorIdList);
        return Result.ok(gasRatioList);
    }


    /**
     * 碳排因子收藏分页列表查询
     *
     * @param emissionFactorQueryDTO 碳排因子查询条件对象
     * @param pageNo
     * @param pageSize
     * @param req
     * @return pageList 碳排因子收藏分页列表查询结果集合
     */
    @Override
    @GetMapping(value = "/getCollectList")
    public Result<?> getCollectList(EmissionFactorQueryDTO emissionFactorQueryDTO,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        IPage<EmissionFactorQueryVO> pageList = cyEmissionFactorCollectService.queryPageList(emissionFactorQueryDTO, pageNo, pageSize);
        return Result.OK(pageList);
    }
    @Override
    @GetMapping(value = "/batchGetGasStore")
    public Result<List<CyGasStore>> batchGetGasStore(){
        //根据碳排因子id集合查询出其下所包含的所有气体配比
        List<CyGasStore> gasRatioList = cyHouseGasService.batchGetAllGasStore();
        return Result.ok(gasRatioList);
    }

    /**
     * 根据原单位和目标单位查询出转换比率
     *
     * @param sourceUnit 原单位
     * @param targetUnit 目标单位
     * @return convertValue 转换比率
     */
    @Override
    @GetMapping(value = "/getConvertValue")
    public Result<Double> getConvertValue(@RequestParam(name="sourceUnit", required = true) String sourceUnit,
                                     @RequestParam(name="targetUnit", required = true) String targetUnit) {

        Double convertValue = cyEmissionUnitConvertService.getConvertValue(sourceUnit, targetUnit);
        return Result.OK(convertValue);
    }




















}
